using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;

namespace BlogExchange
{
    public partial class MainForm : Form
    {
        private List<Entry> fromRssEntries;
        private List<Entry> targetRssEntries;

        public MainForm()
        {
            InitializeComponent();
        }

        private void fromRssLoadButton_Click(object sender, EventArgs e)
        {
            fromRssEntries = LoadRss(fromRssUrlTextBox.Text, this.fromRssListView);
        }

        private void targetRssLoadButton_Click(object sender, EventArgs e)
        {
            targetRssEntries = LoadRss(targetRssUrlTextBox.Text, this.targetRssListView);
        }

        private void moveEntryButton_Click(object sender, EventArgs e)
        {
            if (this.fromRssListView.SelectedItems.Count > 0)
            {
                foreach (ListViewItem selectedItem in this.fromRssListView.SelectedItems)
                {
                    MoveEntry(selectedItem.Tag as Entry);
                }
            }
        }

        private List<Entry> LoadRss(string url, ListView targetListView)
        {
            List<Entry> rssEntries = Reader.Load(url);

            targetListView.Items.Clear();

            foreach (Entry entry in rssEntries)
            {
                ListViewItem item = new ListViewItem(new string[] { entry.Date.ToShortDateString(), entry.Title, entry.Author });
                item.Tag = entry;
                targetListView.Items.Add(item);
            }

            return rssEntries;
        }

        private void MoveEntry(Entry entry)
        {
            if (entry == null)
                return;

            try
            {
                IBlogger blogger = MetaBlogAPIFactory.Create(this.targetMetaBlogUrlTextBox.Text);

                Post post = new Post();
                post.dateCreated = entry.Date;
                post.description = entry.Description;
                post.title = entry.Title;
                post.link = entry.Link;

                blogger.newPost(targetBlogTextBox.Text, targetUsernameTextBox.Text, targetPasswordTextBox.Text, post, publishCheckBox.Checked);

                WriteTrace(string.Format("Post {0} was successfuly moved.", post.title));    
            }
            catch (Exception exc)
            {
                WriteTrace(exc.Message);
                new ErrorDialog(exc).ShowDialog(this);
            }
        }

        private void WriteTrace(string message)
        {
            outputTextBox.Text += message + Environment.NewLine;
        }

        private void fromRssListView_DoubleClick(object sender, EventArgs e)
        {
            if (this.fromRssListView.SelectedItems.Count > 0)
            {
                Entry entry = this.fromRssListView.SelectedItems[0].Tag as Entry;
                if (entry != null)
                {
                    PreviewDialog preview = new PreviewDialog();
                    preview.Navigate(entry.Link);
                    preview.ShowDialog(this);
                }
            }
        }
    }
}